package com.example.bzh.gamecat.ui.cattery.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bzh.gamecat.R;

import java.net.Inet4Address;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class UpdatePersonalNameActivity extends AppCompatActivity {

    @BindView(R.id.iv_update_name_back)
    ImageView ivUpdateNameBack;
    @BindView(R.id.update_personal_name_submit)
    TextView updatePersonalNameSubmit;
    @BindView(R.id.update_personal_name_tv)
    EditText updatePersonalNameTv;
    @BindView(R.id.update_personal_name_num)
    TextView updatePersonalNameNum;
    private Unbinder unbinder;
    private String updateName;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_update_personal_name);
        unbinder = ButterKnife.bind(this);

        Intent intent = getIntent();
        name = intent.getStringExtra("name");

        updatePersonalNameTv.setText(name);
        updatePersonalNameNum.setText(name.length()+"");
        updatePersonalNameTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                int num = updatePersonalNameTv.getText().toString().trim().length();
                updatePersonalNameNum.setText(String.valueOf(num));
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick({R.id.iv_update_name_back,R.id.update_personal_name_submit})
    public void getUpdateOnClick(View view){
        switch (view.getId()){

            case R.id.iv_update_name_back:
                update();
                break;
            case R.id.update_personal_name_submit:
                update();
                break;

        }
    }

    private void update() {
        updateName = updatePersonalNameTv.getText().toString().trim();
        Intent intent = new Intent();
        intent.putExtra("updateName",updateName);
        setResult(4,intent);
        finish();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
